//
//  ViewController.m
//  UIView_Animation
//
//  Created by globalives2 on 2017/2/20.
//  Copyright © 2017年 globalives2. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
// 积分 view
@property (nonatomic, strong) UIImageView *integralView;
// 卡券 view
@property (nonatomic, strong) UIImageView *cartCenterView;
// 签到 view
@property (nonatomic, strong) UIImageView *signInView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.signInView];
    
    [self.view addSubview:self.cartCenterView];
    
    [self.view addSubview:self.integralView];
    
    self.view.backgroundColor = [UIColor blueColor];
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [self flipAni];
    [self blockChangeFrameWithSpringAnimation];
    
}


#pragma mark ******************Block动画方法**********************
//1、普通动画
//下面三段代码都实现了相同的视图frame的变化，不同之处只在于其延迟时间、过渡效果和结束回调。

- (void)blockAni1 {
    [UIView animateWithDuration:1.0 animations:^{
        self.signInView.frame = self.cartCenterView.frame;
    }];
}
- (void)blockAni2 {
    [UIView animateWithDuration:1.0 animations:^{
        self.signInView.frame = self.cartCenterView.frame;
    } completion:^(BOOL finished) {
        NSLog(@"动画结束");
    }];
}
- (void)blockAni3 {
    [UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.signInView.frame = self.cartCenterView.frame;
    } completion:^(BOOL finished) {
        NSLog(@"动画结束");
    }];
}

// 2、Spring动画

- (void)blockAni4 {
    [UIView animateWithDuration:1.0 delay:0.f usingSpringWithDamping:0.5 initialSpringVelocity:5.0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.signInView.frame = self.cartCenterView.frame;
    } completion:^(BOOL finished) {
        NSLog(@"动画结束");
    }];
}

// 弹簧动画
- (void)blockChangeFrameWithSpringAnimation {
    [UIView animateWithDuration:1.0 delay:0.5 usingSpringWithDamping:0.01 initialSpringVelocity:10.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.cartCenterView.frame = self.signInView.frame;
    } completion:^(BOOL finished) {
        self.cartCenterView.hidden = YES;
    }];
}

// 3、Keyframes动画
//这里以实现视图背景颜色变化（红-绿-蓝-紫）的过程来演示关键帧动画。

- (void)blockAni5 {
    self.signInView.image = nil;
    [UIView animateKeyframesWithDuration:9.0 delay:0.f options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.f relativeDuration:1.0 / 4 animations:^{
            self.signInView.backgroundColor = [UIColor colorWithRed:0.9475 green:0.1921 blue:0.1746 alpha:1.0];
        }];
        [UIView addKeyframeWithRelativeStartTime:1.0 / 4 relativeDuration:1.0 / 4 animations:^{
            self.signInView.backgroundColor = [UIColor colorWithRed:0.1064 green:0.6052 blue:0.0334 alpha:1.0];
        }];
        [UIView addKeyframeWithRelativeStartTime:2.0 / 4 relativeDuration:1.0 / 4 animations:^{
            self.signInView.backgroundColor = [UIColor colorWithRed:0.1366 green:0.3017 blue:0.8411 alpha:1.0];
        }];
        [UIView addKeyframeWithRelativeStartTime:3.0 / 4 relativeDuration:1.0 / 4 animations:^{
            self.signInView.backgroundColor = [UIColor colorWithRed:0.619 green:0.037 blue:0.6719 alpha:1.0];
        }];
    } completion:^(BOOL finished) {
        NSLog(@"动画结束");
    }];
}

// 关键 帧 放大 动画
- (void)blockAmplifyAnimation {
    self.signInView.transform = CGAffineTransformIdentity;
    self.signInView.image = [UIImage imageNamed:@"serviceActivity.png"];
    [UIView animateKeyframesWithDuration:1.5 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1/3.0 animations:^{
            self.signInView.transform = CGAffineTransformMakeScale(1.5, 1.5);
        }];
        
        [UIView addKeyframeWithRelativeStartTime:1/3.0 relativeDuration:1/3.0 animations:^{
            self.signInView.transform = CGAffineTransformMakeScale(0.8, 0.8);
        }];
        
        [UIView addKeyframeWithRelativeStartTime:2/3.0 relativeDuration:1/3.0 animations:^{
            self.signInView.transform = CGAffineTransformMakeScale(1.0, 1.0);
        }];
        
    } completion:^(BOOL finished) {
        NSLog(@"动画结束");
    }];
}

//4、转场动画
//4.1 单个视图的过渡效果
// 转场 动画 单个 视图 过渡 效果
- (void)blockTransitionWithSingleViewAnimation {
    [UIView transitionWithView:self.signInView duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        self.cartCenterView.image = [UIImage imageNamed:@"serviceActivity"];
    } completion:^(BOOL finished) {
        NSLog(@"动画结束");
    }];
}

//4.2 从旧视图转到新视图的动画效果

// 转场 动画 (旧视图 到 新 视图)
- (void)blockTransitionFromViewToViewAnimation  {
    UIImageView * newCenterShow = [[UIImageView alloc]initWithFrame:self.signInView.frame];
    newCenterShow.image = [UIImage imageNamed:@"serviceActivity"];
    [UIView transitionFromView:self.signInView toView:newCenterShow duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
        NSLog(@"动画结束");
    }];
}

#pragma mark ****************属性变化动画*****************************

// 转场动画 (curUp) (self.view)
- (void)curUpControllerViewAnimation {
    [UIView beginAnimations:@"FlipAnimation" context:nil];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationWillStartSelector:@selector(startAni:)];
    [UIView setAnimationDidStopSelector:@selector(stopAni:)];
    [UIView setAnimationRepeatCount:1.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
    self.signInView.image = [UIImage imageNamed:@"serviceActivity.png"];
    [UIView commitAnimations];
}

/**
 转场效果动画（以Flip效果为例）
 */
- (void)flipAni {
    [UIView beginAnimations:@"FlipAni" context:nil];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationWillStartSelector:@selector(startAni:)];
    [UIView setAnimationDidStopSelector:@selector(stopAni:)];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.signInView cache:YES];
    
    
    
    self.signInView.image = [UIImage imageNamed:@"serviceActivity"];
    [UIView commitAnimations];
}



/**
 属性变化动画
 */
- (void)changeFrame {
    
    //动画开始标记
    [UIView beginAnimations:@"FrameAni" context:nil];
    //动画持续时间
    [UIView setAnimationDuration:1.0];
    //动画的代理对象
    [UIView setAnimationDelegate:self];
    //设置动画将开始时代理对象执行的SEL
    [UIView setAnimationWillStartSelector:@selector(startAni:)];
    //设置动画结束时代理对象执行的SEL
    [UIView setAnimationDidStopSelector:@selector(stopAni:)];
    //设置动画的重复次数
    [UIView setAnimationRepeatCount:1];

    //设置动画的曲线

    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    self.cartCenterView.frame = self.signInView.frame;
    /// 结束动画标记
    [UIView commitAnimations];
    
    
}

- (void)startAni:(NSString *)aniID {
    NSLog(@"%@ start",aniID);
}

- (void)stopAni:(NSString *)aniID {
    NSLog(@"%@ stop",aniID);
}


#pragma mark --- getter and setter
// 签到 view
- (UIImageView *)signInView {
    if (!_signInView) {
        CGFloat signInViewX = 60;
        CGFloat signInViewY = 120;
        CGFloat signInViewWidth = self.view.frame.size.width - (2 * signInViewX);
        CGFloat signInViewHeight = 100.0f;
        
        _signInView = [[UIImageView alloc] initWithFrame:CGRectMake(signInViewX, signInViewY, signInViewWidth, signInViewHeight)];
        _signInView.clipsToBounds = YES;
        _signInView.contentMode = UIViewContentModeScaleAspectFill;
        _signInView.image = [UIImage imageNamed:@"default_user_icon.png"];
    }
    return _signInView;
}

// 卡券 view
- (UIImageView *)cartCenterView {
    if (!_cartCenterView) {
        
        CGFloat cartCenterViewX = CGRectGetMinX(self.signInView.frame);
        CGFloat cartCenterViewY = CGRectGetMaxY(self.signInView.frame) + 10;
        CGFloat cartCenterViewWidth = self.signInView.frame.size.width/2.0;
        CGFloat cartCenterViewHeight = 60.0f;
        
        _cartCenterView = [[UIImageView alloc] initWithFrame:CGRectMake(cartCenterViewX, cartCenterViewY, cartCenterViewWidth, cartCenterViewHeight)];
        _cartCenterView.contentMode = UIViewContentModeScaleAspectFill;
        _cartCenterView.clipsToBounds = YES;
        _cartCenterView.image = [UIImage imageNamed:@"icon_gouwuche.png"];
    }
    return _cartCenterView;
}

// 积分
- (UIImageView *)integralView {
    if (!_integralView) {
        CGFloat integralViewX = CGRectGetMaxX(self.cartCenterView.frame);
        CGFloat integralViewY = CGRectGetMinY(self.cartCenterView.frame);
        CGFloat integralViewWidth = self.signInView.frame.size.width/2.0;
        CGFloat integralViewHeight = self.cartCenterView.frame.size.height;
        
        _integralView = [[UIImageView alloc] initWithFrame:CGRectMake(integralViewX, integralViewY, integralViewWidth, integralViewHeight)];
        _integralView.clipsToBounds = YES;
        _integralView.contentMode = UIViewContentModeScaleAspectFill;
        _integralView.image = [UIImage imageNamed:@"home_dingdan.png"];
    }
    return _integralView;
}

@end
